package LeetCode;

public class LC_563_BinaryTreeTilt {

    public static void main(String[] args) {

    }

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public class Solution {
        int tilt = 0;

        public int findTilt(TreeNode root) {
            traverse(root);
            return tilt;
        }

        int traverse(TreeNode root) {
            if (root == null) return 0;
            int left = traverse(root.left);
            int right = traverse(root.right);
            tilt += Math.abs(left - right);
            return left + right + root.val;
        }
    }
}